package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.CustomerGroupService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.customergroup.vo.*;
import com.ibigcat.module.mdm.dao.entity.CustomerGroupDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.CustomerGroupMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 客户组管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CustomerGroupServiceImpl implements CustomerGroupService {

    @Resource
    private CustomerGroupMapper customerGroupMapper;

    @Override
    public Long createCustomerGroup(CustomerGroupSaveReqVO createReqVO) {
        // 插入
        CustomerGroupDO customerGroup = BeanUtils.toBean(createReqVO, CustomerGroupDO.class);
        customerGroupMapper.insert(customerGroup);
        // 返回
        return customerGroup.getId();
    }

    @Override
    public void updateCustomerGroup(CustomerGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerGroupExists(updateReqVO.getId());
        // 更新
        CustomerGroupDO updateObj = BeanUtils.toBean(updateReqVO, CustomerGroupDO.class);
        customerGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerGroup(Long id) {
        // 校验存在
        validateCustomerGroupExists(id);
        // 删除
        customerGroupMapper.deleteById(id);
    }

    private void validateCustomerGroupExists(Long id) {
        if (customerGroupMapper.selectById(id) == null) {
            throw exception(CUSTOMER_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public CustomerGroupDO getCustomerGroup(Long id) {
        return customerGroupMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerGroupDO> getCustomerGroupPage(CustomerGroupPageReqVO pageReqVO) {
        return customerGroupMapper.selectPage(pageReqVO);
    }

}